/**
 * @license
*
* PreProcessing is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PreProcessing is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PreProcessing.  If not, see <http://www.gnu.org/licenses/>.
* 
*/

/** 
* @file ConvertingBean.java 
* 
* @brief This file is part of FFT Analysis - PreProcessing. 
* 
* @description This file connects to external libraries
**/
package FFTAnalysis;

import java.beans.*;
import java.io.File;
import java.io.Serializable;
import flanagan.io.*;

/** 
 * @class A component that sets properties for a wave file
 *
 * @author Grant
 * 
 * @version 3
 */
public class ConvertingBean implements Serializable {
    
    private String inputFile;
    private int numChannels;
    private int validBits;
    private long sampleRate;
    private int buf_size;
    private String outputFile;
    
    private PropertyChangeSupport propertySupport;
    
    private static final int NUMCHANNELS = 1;
    private static final int VALIDBITS = 8;
    private static final int BUF_SIZE = 5001;
    private static final int SAMPLERATE = 8000;
    
    
    /** 
     * @brief default constructor
     * 
     * Constructs a default converting bean
     */
    public ConvertingBean()
    {
        inputFile = "";
        numChannels = NUMCHANNELS;
        validBits = VALIDBITS;
        sampleRate = SAMPLERATE;
        buf_size = BUF_SIZE;
        outputFile = "";
        propertySupport = new PropertyChangeSupport(this.outputFile);
    }
    
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.addPropertyChangeListener(listener);
    }
    
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.removePropertyChangeListener(listener);
    }
    
    /**
     * @brief sets the inputFile path
     * @param newInputPath the new Input Path
     */
    public void setInputFile(String newInputPath) 
    {
        
        inputFile = newInputPath;
        this.doConversion();
    }
    
    /**
     * @brief returns the inputFile 
     * @return inputFile of type String
     */    
    public String getInputFile() 
    {
        return inputFile;
    }
    
    /**
     * @brief returns the outputFile
     * @return the outputFile Path of type String
     */    
    public String getOutputFile() 
    {
        return outputFile;
    }
           
        
    /**
    * @brief convert the inputFile.
    * 
    * @description The input file will be converted to the desired sample rate, 
    * number of channels and valid bits.
    *  
    * @ref http://docs.oracle.com/javase/tutorial/sound/converters.html
    */
    public void doConversion()
    {
    try
        {
            File in = new File(inputFile);
            WavFile readWavFile = WavFile.openWavFile(in);          
            int length = in.getName().length();
            String name = in.getName().substring(0, length - 4);
            String oldValue = outputFile;
            outputFile = in.getParentFile().toString();
            outputFile = outputFile.concat("\\").concat(name).concat("_processed.wav");            
            
            //System.out.println(outputFile);   //debugging only
            WavFile writeWavFile = WavFile.newWavFile(new File(outputFile), 
                numChannels, readWavFile.getNumFrames(), validBits, sampleRate);

            double[] buffer = new double[BUF_SIZE * 2];

            int framesRead = 0;
            int framesWritten = 0;

            do
            {
                    framesRead = readWavFile.readFrames(buffer, BUF_SIZE);
                    framesWritten = writeWavFile.writeFrames(buffer, BUF_SIZE);
                    //System.out.printf("%d %d\n", framesRead, framesWritten);
                    //debugging line above
                    }
            while (framesRead != 0);

            readWavFile.close();
            writeWavFile.close();
            //fire the change of property for outputFile
            propertySupport.firePropertyChange("", oldValue, outputFile);
         }
            catch (Exception e)
            {
                System.err.println(e);
                e.printStackTrace();
            }
     }  
    
    
    
}
